<template>
    <h2 style="text-align: center;">登录</h2>
<table>
  <tr>
     <td>账号</td>
     <td><input type="text" placeholder="请输入账号" v-model="userName"></td>
  </tr>
  <tr>
     <td>密码</td>
     <td><input type="password" placeholder="请输入密码" v-model="userPwd"></td>
  </tr>
  <tr>
     <td colspan="2" style="text-align: center;">
       <input type="button" @click="login" value="登录" >
     </td>
  </tr>
</table>
</template>

<script setup lang="ts">
import {ref} from 'vue'
import axios from 'axios'
import {useRouter} from 'vue-router'

const router = useRouter()

//创建响应式数据
const userName = ref('')
const userPwd = ref('')

//登录
const login = ()=>{
 if(userName.value=='')
 {
     alert('请输入账号!')
     return
 }
 if(userPwd.value=='')
 {
     alert('请输入密码!')
     return
 }
 axios.get("https://localhost:44354/api/Invests/UserLogin",{
     params:{
         Uname:userName.value,
         Upwd:userPwd.value
     }
 }).then(res=>{
     if(res.data.RM_Id==200)
     {
        
         SearchUserType(res.data.RM_Data[0].UU_Id)
     }
     else if(res.data.RM_Id==101)
     {
         alert(res.data.RM_Name)
     }
     else if(res.data.RM_Id==201)
     {
         alert(res.data.RM_Name)
     }
 }).catch(err=>{
     console.log(err);
 })
}

//根据用户类型跳转页面
const SearchUserType = (UUid:number)=>{
    axios.get("https://localhost:44354/api/Invests/GetUserRoleType",{
     params:{
        UUid:UUid
     }
 }).then(res=>{
     if(res.data[0].R_Type=="用户")
     {  
        alert('用户登录成功!')
        router.push('/ShowUserInvestInfo')
     }
     else
     {  
        alert('管理员登录成功!')
        router.push('/ShowManagerInvestInfo')
     }
 }).catch(err=>{
     console.log(err);
 })
}
</script>